﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Agents.Admin
{
    //page that Administrater to add a new client
    public partial class NewCostumer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Bind the regions list
                Queries queries = new Queries();
                cmbArea.DataSource = queries.GetAllRegions();
                cmbArea.DataValueField = "Id";
                cmbArea.DataTextField = "Name";
                cmbArea.DataBind();
            }
        }

        //function for redirecting the administrater to his homepage without adding a client
        protected void backBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/AdminMenu.aspx");
        }

        //function activated when the ok button is pressed.
        protected void okBtn_Click(object sender, EventArgs e)
        {
            Queries q = new Queries();

            //making sure all text boxes have been filled
            if (nameTxtbox.Text == "" || txtCity.Text == "" || phoneTxtbox.Text == "" ||
               emailTxtbox.Text == "" || txtStreet.Text == "" || txtStreetNum.Text == ""
               || cmbArea.SelectedItem == null || creditTxtbox.Text == "" || contactMenTxtbox.Text == "" ||
                hpTxtbox.Text == "" || cmbPricesType.SelectedItem == null ||
                (active.Selected == false && nonActive.Selected == false) ||
               (priorityChkBox.Checked && priorityTxtbox.Text == ""))
            {
                //if something is missing- print apropriot message to screan.
                empty.Text = "קיימים שדות ריקים. אנא, מלא את כולם.";
            }
            else if (priorityChkBox.Checked && 
                (int.Parse(priorityTxtbox.Text) < 1 || int.Parse(priorityTxtbox.Text) > 10))
            {
                empty.Text = "עבור תיבת העדיפות- הכנס ערכים בין 1 ל 10.";
            }
            else
            {   //making sure the new client does not exist in our data base.
                if (q.ClientExists(nameTxtbox.Text) == false)
                {
                    // Disable the buttons so the user can't interfere
                    backBtn.Enabled = false;
                    okBtn.Enabled = false;

                    string priority = "-1";
                    string status = "פעיל";
                    if (priorityChkBox.Checked)
                        priority = priorityTxtbox.Text;
                    if (nonActive.Selected)
                        status = "לא פעיל";

                    //adding the new client into our database
                    q.AddClient(nameTxtbox.Text, txtCity.Text, txtStreet.Text,
                        txtStreetNum.Text, phoneTxtbox.Text,
                        emailTxtbox.Text, priority, Int32.Parse(cmbArea.SelectedValue),
                        creditTxtbox.Text, contactMenTxtbox.Text, hpTxtbox.Text,
                        Int32.Parse(cmbPricesType.SelectedValue), status, 3.12, 3.45);

                    // Announce success and wait for 2 seconds before redirecting
                    empty.Text = "הלקוח נוסף בהצלחה";
                    redirectionTimer.Enabled = true;
                }
                //if the client already exists- print apropriate message to screan
                else
                {
                    nameExists.Text = "שם הלקוח כבר נמצא במערכת";
                }
            }
        }

        /// <summary>
        /// Redirect back to the admin menu after a few seconds
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void redirectionTimer_Tick(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/AdminMenu.aspx");
        }
    }
}